package org.shopen.ai.puremvc.model.settings
{
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.shopen.ai.puremvc.model.utils.ICompleteDelegate;

	public class SettingsProxy extends Proxy
	{
		public static const NAME : String = "SettingsProxy";
		
		public static const COMMIT_MANUALSETTINGS_RESULT : String 	= NAME + ".COMMIT_MANUALSETTINGS_RESULT";
		public static const COMMIT_AUTOSETTINGS_RESULT : String 	= NAME + ".COMMIT_AUTOSETTINGS_RESULT";
		
		public static const MANUALSETTINGS_CHANGED : String 		= NAME + ".MANUALSETTINGS_CHANGED";
		public static const AUTOSETTINGS_CHANGED : String 			= NAME + ".AUTOSETTINGS_CHANGED";
		
		public static const MANUALSETTINGS_LOADED : String 			= NAME + ".MANUALSETTINGS_LOADED";
		public static const AUTOSETTINGS_LOADED : String 			= NAME + ".AUTOSETTINGS_LOADED";
		
		public static const PERFORMMODES_LOADED : String			= NAME + ".PERFORMMODES_LOADED";
		
		protected var _activeSettingsType : SettingsType = SettingsType.MANUAL;
		public function get activeSettingsType() : SettingsType
		{
			if (_activeSettingsType == null)
			{
				_activeSettingsType = SettingsType.MANUAL;
			}
			return _activeSettingsType;
		}
		
		public function get currentSettings() : ISettings
		{
			var result : ISettings = null;
			switch (activeSettingsType)
			{
				case SettingsType.AUTOMATIC:
					result = autoSettings;
					break;
				case SettingsType.MANUAL:
					result = manualSettings;
					break;
			}
			return result;
		}
		
		public function areCurrentSettingsValid() : Boolean
		{
			var result : Boolean = false;
			if (currentSettings != null)
			{
				result = currentSettings.isValid();
			}
			return result;
		}
		
		protected var _manualSettings : ManualSettings;
		public function get manualSettings() : ManualSettings
		{
			return _manualSettings;
		}
		
		protected var _autoSettings : AutoSettings;
		public function get autoSettings() : AutoSettings
		{
			return _autoSettings;
		}
		
		protected var _performModes : ArrayCollection;
		public function get performModes() : ArrayCollection
		{
			return _performModes;
		}
		
		public function SettingsProxy()
		{
			super(SettingsProxy.NAME);
		}
		
		override public function getProxyName():String
		{
			return SettingsProxy.NAME;
		}
		
		public function loadPerformModes(completeDelegate : ICompleteDelegate, type : String = null) : void
		{
			if (performModes == null)
			{
				_performModes = PerformModeType.getAllModes();
			}
			sendNotification(PERFORMMODES_LOADED, performModes, type);
			completeDelegate.dispatchComplete();
		}
		
		public function loadManualSettings(type : String = null) : void
		{
			if (manualSettings == null)
			{
				_manualSettings = new ManualSettings(null, null, null, PerformModeType.STEPS, 5);
			}
			sendNotification(MANUALSETTINGS_LOADED, manualSettings, type);
		}
		
		public function loadAutoSettings(type : String = null) : void
		{
			if (autoSettings == null)
			{
				_autoSettings = new AutoSettings(new ArrayCollection(), 1);
			}
			sendNotification(AUTOSETTINGS_LOADED, autoSettings, type);
		}
		
		public function commitManualSettings(settings : ManualSettings, type : String = null) : void
		{
			var result : ICommitSettingsResult = validateSettings(settings);
			if (result.isValid)
			{
				_manualSettings = settings;
			}
			sendNotification(COMMIT_MANUALSETTINGS_RESULT, result, type);
		}
		
		public function commitAutoSettings(settings : AutoSettings, type : String = null) : void
		{
			var result : ICommitSettingsResult = validateSettings(settings);
			if (result.isValid)
			{
				_autoSettings = settings;
			}
			sendNotification(COMMIT_AUTOSETTINGS_RESULT, result, type);
		}
		
		public function changeActiveSettings(type : SettingsType) : void
		{
			this._activeSettingsType = type;
		}
		
		public function validateSettings(settings : ISettings) : ICommitSettingsResult
		{
			var result : ICommitSettingsResult;
			var errors : ArrayCollection;
			if (settings == null)
			{
				errors = new ArrayCollection(["Settings have not been provided!"]);
			}
			else
			{
				errors = settings.invalidFields;
			}
			result = new CommitSettingsResult(errors);
			return result;
		}
	}
}